package cn.stylefeng.guns.modular.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.system.entity.SysConfType;
import cn.stylefeng.guns.modular.system.mapper.SysConfTypeMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class SystemConfService {

	@Autowired
	private SysConfTypeMapper sysConfTypeMapper;
	
	public Page<Map<String, Object>> list(String name,String type) {
        Page page = LayuiPageFactory.defaultPage();
        return sysConfTypeMapper.list(page, name, type);
    }
	
    /**
     * 新增系统配置参数
     */
    @Transactional(rollbackFor = Exception.class)
    public void addSysConfType(SysConfType sysConfType) {
        if (ToolUtil.isOneEmpty(sysConfType, sysConfType.getName(),sysConfType.getName())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        sysConfType.setId(UUID.randomUUID().toString());
        sysConfType.setValid(1);
        sysConfTypeMapper.insert(sysConfType);
    } 
	

    /**
     * 判断配置编码是否存在
     * @param type
     * @return
     */
    public boolean typeIsExist(String type){
    	Map<String, Object> condition = new HashMap<>();
    	condition.put("type", type);
    	List<SysConfType> selectByMap = sysConfTypeMapper.selectByMap(condition);
    	if (selectByMap==null||selectByMap.isEmpty()) {
			return false;
		}else {
			return true;
		}
    }
    
    
	
}
